package experiment;

import itemMining.FullyCorrelatedItem;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import thesis.DataObject;
import thesis.DataUtil;
import thesis.FSModule;
import thesis.InfoUnit;

public class Preprocess {

	public static double[][] prepareForSummarization(
			HashMap<Long, DataObject> objects, List<Dimension> dimensions,
			ArrayList<Double> weights, ArrayList<InfoUnit> infoUnits) {
		// --------------------convert attributes to info units---------------
		int elemIU = 0;
		int intra_corr = 0;
		for (Dimension dim : dimensions) {
			int ori_size = dim.value2obj.size();
			elemIU += ori_size;
			dim.generateBIU();
			intra_corr += dim.value2obj.size() - ori_size;
		}
		System.out.println("Elemental IU: " + elemIU);
		System.out.println("Intra_corr: " + intra_corr);
		int internId = 0;
		for (DataObject t : objects.values()) {
			t.setInternId(internId++);
			t.initRels(dimensions, weights);
		}
		// assign relevance score for each memoryTweet
		int dimCount = 0;
		for (Dimension table : dimensions) {
			// add relscore in the order of keys in the hashtable
			for (String key : table.value2obj.keySet()) {
				HashMap<Long, Double> rels = table.value2obj.get(key);
				// make small probability to 0
				for (Map.Entry<Long, Double> entry : rels.entrySet()) {
					if (entry.getValue() < FSModule.relThresh) {
						entry.setValue(0.0);
					}
				}
				for (DataObject t : objects.values()) {
					Double rel = rels.get(t.getDbId());
					t.addRel(dimCount, rel == null ? 0 : rel);
				}
			}
			dimCount++;
		}

		// generate information units
		ArrayList<InfoUnit> tmpInfoUnits = new ArrayList<InfoUnit>();
//		 double[][] probs = DataUtil.marginal2Joint(
//		 FSModule.maxInfoSize == 0 ? dimensions.size()
//		 : FSModule.maxInfoSize, dimensions, objects,
//		 tmpInfoUnits);

		double[][] probs = new FullyCorrelatedItem().marginal2Joint(
				FSModule.maxInfoSize == 0 ? dimensions.size()
						: FSModule.maxInfoSize, dimensions, objects,
				tmpInfoUnits, Parameter.crossCorrThresh);

		for (InfoUnit unit : tmpInfoUnits) {
			double totalTweets = 0;
			for (DataObject t : objects.values()) {
				double prob = probs[t.getInternId()][unit.getInternId()];
				if (prob > 0) {
					t.addUnit(unit, prob);
				}
				totalTweets += prob;
			}
			if (totalTweets > 0) {
				infoUnits.add(unit);
			}
			// System.out.println(unit + " : " + totalTweets
			// / (double) objects.size());
		}
		System.out.println("Size of info units: " + infoUnits.size());
		return probs;

	}

}
